/*
 * Project: Sudoku Explainer
 * Copyright (C) 2006-2007 Nicolas Juillerat
 * 
 * Project: Sudoku Hint Engine for Android
 * Copyright (C) 2010-2013 Emanuel Moecklin
 * 
 * Available under the terms of the Lesser General Public License (LGPL)
 */
package com.onegravity.sudoku.solver.rules.hidden;

import java.util.Collection;

import android.net.Uri;

import com.onegravity.sudoku.solver.HintResource;
import com.onegravity.sudoku.solver.HintType;
import com.onegravity.sudoku.solver.core.DirectHint;
import com.onegravity.sudoku.solver.core.Rule;
import com.onegravity.sudoku.solver.data.Block;
import com.onegravity.sudoku.solver.data.Cell;
import com.onegravity.sudoku.solver.data.Column;
import com.onegravity.sudoku.solver.data.Region;
import com.onegravity.sudoku.solver.data.Row;
import com.onegravity.sudoku.solver.util.HtmlLoader;

/**
 * Hidden Single hint
 */
public class HiddenSingleHint extends DirectHint implements Rule {

	private static final long serialVersionUID = -637211617441619733L;
	
	private final boolean isAlone; // last empty cell in a region

    public HiddenSingleHint(Region region, Cell cell, int value, boolean isAlone, Collection<Cell> selectedCells) {
        super(region, cell, value, selectedCells, null);
        this.isAlone = isAlone;
    }

    @Override
    public double getDifficulty() {
    	Region region = getRegions(HintType.HINT)[0];
        return isAlone ? 1.0 :
        	   (region instanceof Block) ? 1.2 :
           	   (region instanceof Row) ? 1.5 :
           	   (region instanceof Column) ? 1.5 :
           	   1.8;
    }

    @Override
    public String getName() {
    	return isAlone ? HintResource.FULL_HOUSE.getName() : (HintResource.HIDDEN_SINGLE.getName() + " (" + super.getRegions(HintType.HINT)[0] + ")"); //$NON-NLS-1$ //$NON-NLS-2$
    }

    @Override
    public Uri getLink() {
    	return isAlone ? HintResource.FULL_HOUSE.getUrl() : HintResource.HIDDEN_SINGLE.getUrl();
	}

    @Override
    public Collection<Cell> getSelectedCells(HintType hintType) {
    	return (hintType!=HintType.SMALL_CLUE) ? super.getSelectedCells(hintType) : null;
    }

    @Override
    public Cell getCell(HintType type) {
        return (type==HintType.HINT) ? super.getCell(type) : null;
    }

    @Override
    public String getHintHtml(HintType type) {
    	HintResource res = isAlone ? HintResource.FULL_HOUSE : HintResource.HIDDEN_SINGLE;
        String result = HtmlLoader.loadHtml(res.hint(type));
        return HtmlLoader.format(result, super.getCell(type).toString(), Integer.toString(super.getValue()), super.getRegions(HintType.HINT)[0].toFullString());
    }

    @Override
    public String toString() {
        return getName() + ": " + super.toString(); //$NON-NLS-1$
    }

    @Override
    public int getNrOfSteps() {
    	return 3;
    }

}